﻿<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); 
header("Content-type: text/xml;  charset=TIS-620");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header
header("content-type:text/xml"); 
createKmlFile();
function parseToXML($htmlStr)
{ 

$xmlStr=str_replace('<','&lt;',$htmlStr); 

$xmlStr=str_replace('>','&gt;',$xmlStr); 

$xmlStr=str_replace('"','&quot;',$xmlStr); 

$xmlStr=str_replace("'",'&#39;',$xmlStr); 

$xmlStr=str_replace("&",'&amp;',$xmlStr); 
$xmlStr=str_replace("",'a',$xmlStr); 


return $xmlStr; 

} 

function xmllist($result,$sql2) {
	while ($row1 = mysql_fetch_array($result)) {
		// $sqlProject="SELECT * FROM project WHERE idStu = '$idstu'"
		echo '<cd>';
		echo '<id1>'.parseToXML($row1['NPro']).'</id1>';
			//if($row1['bossproject']!=NULL)
		echo '<id2>'.parseToXML($row1['bossproject']).'</id2>';
		//else
		//echo '<id2> </id2>';
		//if($row1['id']!=NULL)
		if ($sql2 != 1)
		echo '<id3>'.parseToXML($row1['id']).'</id3>';
		else
		echo '<id3> </id3>';
		echo '<id4>'.parseToXML($row1['idStu']).'</id4>';
		echo '<id5>'.parseToXML($row1['idPro']).'</id5>';
		echo '<id6>'.parseToXML($row1['dateStrat']).'</id6>';
		$checkTime = YTime($row1['dateEnd']); //
		
		echo '<id7>'.$checkTime.'</id7>';
		if ($sql2 != 1) {
			
			echo '<id8>'.parseToXML($row1['name']).'</id8>';
			//echo '<id9>'.parseToXML($row1['name']).'</id9>';
		}
		echo '</cd>';
		
	}
}

function YTime($time){ // เวลาในรูปแบบ มาตรฐาน 2006-03-27 21:39:12 
$timeArr["Y"]= substr($time,2,2);


return $timeArr["Y"];
}
function createKmlFile(){

$q=$_GET["q"];
//include('time.php');//เรียกไฟล์เวลา
$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("aris", $con);
mysql_query("SET NAMES UTF8");
$sql="SELECT * FROM student WHERE id_st = $q";

$result = mysql_query($sql);
echo '<?xml version="1.0" encoding="utf-8"?>';  
//echo "<input name='name_st' type='text' value='" ;
header("Content-type: text/xml");
echo "<CATALOG>";

//<table id='box-table-a' cellspacing='0' cellpadding='0'>
//<tr>
//<th>Firstname</th>
//<th>Lastname</th>
//<th>Age</th>
//<th>Hometown</th>
//<th>Job</th>
//</tr>";
//
echo '2:'.mysql_num_rows($result);//ใช้นับจำนวนแถวทั้งหมดของ result
if(mysql_num_rows($result)>0)
{
while($row = mysql_fetch_array($result))
  {

  
  $idstu = $row['id'];
	echo '<cd>';
	echo '<id1>'.parseToXML($row['name_st']).'</id1>';
	echo '<id2>'.parseToXML($row['class_st']).'</id2>';
	echo '<id3>'.parseToXML($row['Pre_st']).'</id3>';
	echo '<id4>'.parseToXML($row['sname_st']).'</id4>';
	echo '<id5>'.parseToXML($row['id']).'</id5>';
    echo '</cd>';
	
}
$sqlTimeLogOut="select * from (SELECT project.`NPro` , project.`bossproject` , machine.`name` , machine.`id`
, result.`dateStrat` , result.`dateEnd` ,result.idStu,result.idPro FROM result 
INNER JOIN project ON result.idPro = project.idPro 
inner join machine On result.idtool=machine.id
where result.idStu='$idstu'and  result.dateEnd='0000-00-00 00:00:00'
 ORDER BY `project`.`bossproject` ASC )x ";
$result = mysql_query($sqlTimeLogOut);
echo $sqlTimeLogOut;
$sql2=0;
xmllist($result,$sql2);
//if(mysql_num_rows($result)==0)
//{
$sql="select *  from (SELECT project.`NPro` , project.`bossproject` , result.`idTool` , result.`dateStrat` , result.`dateEnd`
,result.idStu,result.idPro
FROM result

INNER JOIN project
 ON result.idPro = project.idPro
where result.idStu='$idstu'
ORDER BY `project`.`bossproject` ASC
)x GROUP BY NPro";
// $sql="SELECT * FROM result WHERE idStu = '$idstu'";
//echo "1:".mysql_data_seek ( $result, 0 );
$sql2=1;

$result = mysql_query($sql);
//}
xmllist($result,$sql2);
/*
while($row1 = mysql_fetch_array($result))
  {
 // $sqlProject="SELECT * FROM project WHERE idStu = '$idstu'"
  echo 		'<cd>';
	echo '<id1>'.parseToXML($row1['NPro']).'</id1>';
	if($row1['bossproject']!=NULL)
	echo '<id2>'.parseToXML($row1['bossproject']).'</id2>';
	else
	echo '<id2> </id2>';
	echo '<id3>'.parseToXML($row1['id']).'</id3>';
	echo '<id4>'.parseToXML($row1['idStu']).'</id4>';
    echo '<id5>'.parseToXML($row1['idPro']).'</id5>';
    echo '<id6>'.parseToXML($row1['dateStrat']).'</id6>';
	$checkTime=YTime($row1['dateEnd']);//
	
    echo '<id7>'.$checkTime.'</id7>';
	if($sql2!=1){
	
	echo '<id8>'.parseToXML($row1['name']).'</id8>';
	//echo '<id9>'.parseToXML($row1['name']).'</id9>';
	}
    echo '</cd>';  

  }
  
  */
  
  
  }
  else
  {
	echo '<cd>';
	echo '<id1>0</id1>';
	echo '<id2>0</id2>';
	echo '<id3>0</id3>';
	echo '<id4>0</id4>';
    echo '<id5>0</id5>';   
    echo '</cd>';              
  }
  
		$sql="SELECT
liststgroup.idGroup,
projectgroup.idG,
projectgroup.typeSub,
projectgroup.sub1,
projectgroup.sub2
FROM
liststgroup
INNER JOIN projectgroup ON liststgroup.idGroup = projectgroup.idG
WHERE
liststgroup.idSt = '$idstu'";
$result = mysql_query($sql);
while ($row1 = mysql_fetch_array($result)) {
		// $sqlProject="SELECT * FROM project WHERE idStu = '$idstu'"
		echo '<group>';
		echo '<id1>'.parseToXML($row1['idGroup']).'</id1>';
			//if($row1['bossproject']!=NULL)
		echo '<id2>'.parseToXML($row1['typeSub']).'</id2>';
		echo '<id3>'.parseToXML($row1['sub1']).'</id3>';
		echo '<id4>'.parseToXML($row1['sub2']).'</id4>';
		echo '</group>';

}

echo '</CATALOG>';
//$row1 = mysql_fetch_array($result,0,0)
 // echo "<tr>";
  //echo  $row['name_st'] . "";
  //echo "<td>" . $row['sname_st'] . "</td>";
  //echo "<td>" . $row['class_st'] . "</td>";
  //echo "<td>" . $row['date_st'] . "</td>";
 // echo "<td>" . $row['id'] . "</td>";
 // echo "</tr>";
  //}
//echo "'>";
//echo "</CATALOG>";
 //$idstu=$row1;

mysql_close($con);
}
?>